package tom.vertx.core.demo12;

import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.Json;
import io.vertx.core.parsetools.JsonParser;
import lombok.Data;

import java.io.Serializable;

/**
 * Json解析器，可用于解析大结构体json
 *
 * @author ZHUFEIFEI
 */
public class JsonParserDemo {

  public static void main(String[] args) {
    JsonParser parser = JsonParser.newParser().handler(event -> {
      User user = event.mapTo(User.class);
      System.out.println(" User => " + user.firstName + "-" + user.lastName);
    })
      .exceptionHandler(exp -> {
        exp.printStackTrace();
      })
      //json Object模式
      .objectValueMode();
    parser.handle(Buffer.buffer("{\"firstName\":\"tom\",\"lastName\":\"cat\"}"));
    //json数据结束
    parser.end();
  }

  @Data
  static class User implements Serializable {
    String firstName;
    String lastName;
  }

  public static void main111(String[] args) {
    JsonParser parser = JsonParser.newParser();
    //array 数组模式
    parser.handler(event -> {
      switch (event.type()) {
        case START_OBJECT:
          // Set array value mode to handle each entry, from now on the parser won't emit start array events
          parser.arrayValueMode();
          break;
        case VALUE:
          // Handle each array
          // Get the field in which this object was parsed
          System.out.println("Value : " + event.value());
          break;
        case END_OBJECT:
          // Set the array event mode so the parser emits start/end object events again
          parser.arrayEventMode();
          break;
      }
    });
    parser.handle(Buffer.buffer("[0,1,2,3,4]"));
    parser.end();
  }

  public static void main11(String[] args) {
    JsonParser parser = JsonParser.newParser();
    //object模式，读取jsonObject
    parser.handler(event -> {
      switch (event.type()) {
        case START_OBJECT:
          // Set object value mode to handle each entry, from now on the parser won't emit start object events
          parser.objectValueMode();
          break;
        case VALUE:
          // Handle each object
          // Get the field in which this object was parsed
          String name = event.fieldName();
          System.out.println("User with " + name + " : " + event.value());
          break;
        case END_OBJECT:
          // Set the object event mode so the parser emits start/end object events again
          parser.objectEventMode();
          break;
      }
    });
    parser.handle(Buffer.buffer("{\"name\":{\"firstName\":\"Bob\",\"lastName\":\"Morane\"}}"));
    parser.end();
  }

  public static void main1(String[] args) {
    JsonParser parser = JsonParser.newParser();
    parser.handler(event -> {
      switch (event.type()) {
        case START_OBJECT:
          // Start an objet
          System.out.println("start object => " + Json.encode(event));
          break;
        case END_OBJECT:
          // End an objet
          System.out.println("end object => " + Json.encode(event.objectValue()));
          break;
        case START_ARRAY:
          // Start an array
          System.out.println("start array => " + Json.encode(event));
          break;
        case END_ARRAY:
          // End an array
          System.out.println("end array => " + Json.encode(event.arrayValue()));
          break;
        case VALUE:
          // Handle a value
          String field = event.fieldName();
          if (field != null) {
            // In an object
            System.out.println("NAME => " + field + " VALUE => " + event.stringValue());
          } else {
            // In an array or top level
            if (event.isString()) {
              System.out.println("string value => " + event.stringValue());
            } else {
              // ...
              System.out.println("event => " + event);
            }
          }
          break;
      }
    });

    parser.exceptionHandler(exp -> {
      exp.printStackTrace();
    });

    // start array event
    // start object event
    // "firstName":"Bob" event
    parser.handle(Buffer.buffer("[{\"firstName\":\"Bob\","));

    // "lastName":"Morane" event
    // end object event
    parser.handle(Buffer.buffer("\"lastName\":\"Morane\"},"));

    // start object event
    // "firstName":"Luke" event
    // "lastName":"Lucky" event
    // end object event
    parser.handle(Buffer.buffer("{\"firstName\":\"Luke\",\"lastName\":\"Lucky\"}"));

    // end array event
    parser.handle(Buffer.buffer("]"));

    // Always call end
    parser.end();
  }

}
